package com.tools_s7.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.tools.s7.base.CompatActivity;
import com.tools.s7.recycler.DividerItemDecoration;
import com.tools.s7.recycler.utils.LoadMoreHelper;
import com.tools.s7.utils.StatusBarUtils;
import com.tools.s7.utils.StringUtils;
import com.tools_s7.R;
import com.tools_s7.adapter.TestAdapter;
import com.tools_s7.mvp.PMActivity;
import com.tools_s7.service.HintService;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends CompatActivity implements View.OnClickListener {

    private Button btn1, btn2, btn3, btn4, btn5, btn6;
    private SwipeRefreshLayout swipeRefresh;
    private RecyclerView recycler;
    private LinearLayoutManager layoutManager;
    private List<String> datas;

    private LoadMoreHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        StatusBarUtils.getInstance(this).setStatusBar(0xFFCCFFFF);

        btn1 = findViewById(R.id.btn_1);
        btn2 = findViewById(R.id.btn_2);
        btn3 = findViewById(R.id.btn_3);
        btn4 = findViewById(R.id.btn_4);
        btn5 = findViewById(R.id.btn_5);
        btn6 = findViewById(R.id.btn_6);
        swipeRefresh = findViewById(R.id.swipeRefresh);
        recycler = findViewById(R.id.recycler);

        /**
         * 启动服务
         */
        stopService(new Intent(this,HintService.class));

        datas = new ArrayList<>();

        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mHelper = new LoadMoreHelper.Builder<String>()
                .setSwipeRefresh(swipeRefresh)
                .setPageSize(10)
                .setRecycler(recycler, layoutManager, new DividerItemDecoration(this, 5, 0xFFFF0000))
                .setAdapter(new TestAdapter(), true)
                .setLoadData(new LoadMoreHelper.LoadDataListener() {
                            @Override
                            public void onLoadData(int page) {
                                getData(page);
                    }
                }).build();

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                startActivity(PMActivity.class);
                break;
            case R.id.btn_2:
                startActivity(TestActivity.class);
                break;
            case R.id.btn_3:
                startActivity(StaggeredActivity.class);
                break;
            case R.id.btn_4:
                startActivity(HttpActivity.class);
                break;
            case R.id.btn_5:
                startActivity(ServiceActivity.class);
                break;
            case R.id.btn_6:
                StringUtils.setPushToast(this, "12312","1`321321");
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        index = 10;
        mHelper.loadFirstPage();
    }

    /**
     * 数据下标
     */
    private int s = 0;
    /**
     * 记录执行次数
     */
    private int index = 10;

    public void getData(int page) {
        if (page == 1) {
            s = 0;
        }

        int e = page * 10;

        /**
         * 第一页测试数剧为空时逻辑
         */
        if (page == 1 && index == 10) {
            index = 0;
            onNull(3);
            return;
        }

        /**
         * 第二页测试再试一次
         */
        if (page == 2 && index == 0) {
            index = 1;
            onLoadError(3);
            return;
        }

        /**
         * 最后一页测试没有更多数据
         */
        if (page == 4) {
            e = e - 3;
        }

        datas.clear();

        for (int i=s; i<e; i++) {
            datas.add("你好" + i);
        }
        s = e;
        CountDown(3);
    }

    public void onNull(int s) {
        new CountDownTimer(s * 1000, 1000) {
            @Override
            public void onTick(long l) {
            }
            @Override
            public void onFinish() {
                mHelper.notifyNull();
            }
        }.start();
    }

    public void onLoadError(int s) {
        new CountDownTimer(s * 1000, 1000) {
            @Override
            public void onTick(long l) {
            }
            @Override
            public void onFinish() {
                mHelper.notifyError();
            }
        }.start();
    }

    public void CountDown(int s) {
        new CountDownTimer(s * 1000, 1000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                mHelper.notifyAdapter(datas);
            }
        }.start();
    }
}
